package com.cn.ant.modules.score.web;

import com.cn.ant.common.config.Global;
import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.DateUtils;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.utils.excel.ExportExcel;
import com.cn.ant.common.utils.excel.ImportExcel;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.school.entity.School;
import com.cn.ant.modules.school.entity.SchoolMajor;
import com.cn.ant.modules.school.service.SchoolMajorService;
import com.cn.ant.modules.school.service.SchoolService;
import com.cn.ant.modules.score.entity.LqMajor;
import com.cn.ant.modules.score.service.LqMajorService;
import com.cn.ant.modules.sys.entity.Area;
import com.cn.ant.modules.sys.service.AreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专业录取线Controller
 * @author AntDream
 * @version 2015-08-28
 */
@Controller
@RequestMapping(value = "${adminPath}/score/lqMajor")
public class LqMajorController extends BaseController {

	@Autowired
	private LqMajorService lqMajorService;
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private SchoolMajorService schoolMajorService;
	
	@ModelAttribute
	public LqMajor get(@RequestParam(required=false) String id) {
		LqMajor entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = lqMajorService.get(id);
		}
		if (entity == null){
			entity = new LqMajor();
		}
		return entity;
	}
	
	@RequiresPermissions("score:lqMajor:view")
	@RequestMapping(value = {"list", ""})
	public String list(LqMajor lqMajor, HttpServletRequest request, HttpServletResponse response, Model model) {
		String schoolCode = request.getParameter("schoolCode");
		lqMajor.setSchoolCode(schoolCode);
		Page<LqMajor> page = lqMajorService.findPage(new Page<LqMajor>(request, response), lqMajor);
		Map<String, String> params = new HashMap<>();
		params.put("type", "2");
		List<Area> provinceList = areaService.query(params);
		model.addAttribute("provinceList", provinceList);
		model.addAttribute("page", page);
		return "modules/score/lqMajorList";
	}

	@RequiresPermissions("score:lqMajor:view")
	@RequestMapping(value = "form")
	public String form(LqMajor lqMajor,HttpServletRequest request, Model model) {
		String schoolCode = request.getParameter("schoolCode");
		String schoolCodeSn = request.getParameter("schoolCodeSn");
		if (StringUtils.isNotBlank(schoolCode)) {
			School school = schoolService.getByCode(schoolCode, schoolCodeSn);
			lqMajor.setSchoolCode(school.getSchoolCode());
			lqMajor.setSchoolName(school.getSchoolName());
		}
		//获取省份信息
		Map<String, String> params = new HashMap<>();
		params.put("type", "2");
		List<Area> provinceList = areaService.query(params);
		//获取学校对应的专业
		List<SchoolMajor> schoolMajorList = schoolMajorService.queryBySchoolCode(schoolCode);
		model.addAttribute("provinceList", provinceList);
		model.addAttribute("schoolMajorList", schoolMajorList);
		model.addAttribute("lqMajor", lqMajor);
		return "modules/score/lqMajorForm";
	}

	@RequiresPermissions("score:lqMajor:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(LqMajor lqMajor, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, lqMajor)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
                return jo;
			} else {
			    lqMajorService.save(lqMajor);
			}
			msg = "保存专业录取线成功";
            jo.setMsg(msg);
            jo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("score:lqMajor:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxJson delete(LqMajor lqMajor, RedirectAttributes redirectAttributes) {
		AjaxJson jo = new AjaxJson();
		if (StringUtils.isNotBlank(lqMajor.getId())) {
			lqMajorService.delete(lqMajor);
			jo.setMsg("删除专业录取线成功");
		} else {
			jo.setSuccess(false);
			jo.setMsg("参数错误,删除失败");
		}
		return jo;
	}

	@RequiresPermissions("score:lqMajor:edit")
	@RequestMapping(value = "export", method= RequestMethod.POST)
	public String exportFle(LqMajor lqMajor, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = lqMajor.getSchoolName()+"专业录取线数据数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Map<String, Object> params = new HashMap<String, Object>();
			Page<LqMajor> page = lqMajorService.findPage(new Page<LqMajor>(request, response, -1), lqMajor);
			new ExportExcel("专业录取线数据", LqMajor.class).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出专业录取线数据失败！失败信息：" + e.getMessage());
			e.printStackTrace();
		}
		return "redirect:"+ Global.getAdminPath()+"/score/lqMajor/?repage";
	}

	@RequiresPermissions("score:lqMajor:edit")
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "专业录取线数据导入模板.xlsx";
			Page<LqMajor> page = lqMajorService.findPage(new Page<LqMajor>(1, 1), new LqMajor());
			new ExportExcel("专业录取线数据", LqMajor.class, 2).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
			e.printStackTrace();
		}
		return "redirect:"+Global.getAdminPath()+"/score/lqMajor/?repage";
	}

	@RequiresPermissions("score:lqMajor:edit")
	@RequestMapping(value = "import", method=RequestMethod.POST)
	public String importFile(MultipartFile file, HttpServletRequest request) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<LqMajor> list = ei.getDataList(LqMajor.class);
			//lqSchoolService.batchSave(list);
			logger.info("导入专业录取线成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:"+Global.getAdminPath()+"/score/lqMajor/?repage";
	}

}